<?php


namespace app\backstage\controller;


use app\model\Area as AreaModel;
use app\util\ReturnCode;
use app\util\Tools;
use app\util\Express;

class Area extends Base {
    public function upload(){
        ini_set("max_execution_time", 2400); // s 40 分钟
//修改此次的最大运行内存
ini_set("memory_limit", 1048576000); // Byte 1000 兆，即 1G
         $data = file_get_contents('C:\Users\Administrator\Desktop\image\area.json');
         $jsonData = json_decode($data, true);
         $model = new AreaModel;
        foreach($jsonData as $var){
            $sql = "INSERT into a_area (`id`, `pid`,`name`, `label`, `level`)value('{$var['code']}',0, '{$var['name']}','".self::getFirstCharter($var['name'])."',1)";
            $model->execute($sql);
            if(isset($var['children'])){
                foreach( $var['children'] as $cvar){
                    $sql = "INSERT into a_area (`id`, `pid`,`name`, `label`, `level`)value('{$cvar['code']}','{$var['code']}', '{$cvar['name']}','".self::getFirstCharter($cvar['name'])."',2)";
                    $model->execute($sql);
                    if(isset($cvar['children'])){
                        foreach( $cvar['children'] as $ccvar){
                            $sql = "INSERT into a_area (`id`, `pid`,`name`, `label`, `level`)value('{$ccvar['code']}','{$cvar['code']}', '{$ccvar['name']}','".self::getFirstCharter($ccvar['name'])."',3)";
                            $model->execute($sql);
                            if(isset($ccvar['children'])){
                                foreach( $ccvar['children'] as $cccvar){
                                    $sql = "INSERT into a_area (`id`, `pid`,`name`, `label`, `level`)value('{$cccvar['code']}','{$ccvar['code']}', '{$cccvar['name']}','".self::getFirstCharter($cccvar['name'])."',4)";
                                    $model->execute($sql);
                                }
                            }
                        }
                    }
                }
            }
        }
    }


   public function  areaList(){
        $Express =  new Express();
        $Express->setType('中通快递');
        $Express->setNumber('75434795060018');
        print_r($Express->map('长沙鑫远玲珑3栋1单元101店'));
        die;
   }

    static function getFirstCharter($str){ 
        if (empty($str)) {
            return '';
        }

        $fir = $fchar = ord($str[0]);
        if ($fchar >= ord('A') && $fchar <= ord('z')) {
            return strtoupper($str[0]);
        }

        $s1 = @iconv('UTF-8', 'gb2312//IGNORE', $str);
        $s2 = @iconv('gb2312', 'UTF-8', $s1);
        $s = $s2 == $str ? $s1 : $str;
        if (!isset($s[0]) || !isset($s[1])) {
            return '';
        }
        $asc=ord($s{0})*256+ord($s{1})-65536; 
        if($asc>=-20319&&$asc<=-20284) return 'A'; 
        if($asc>=-20283&&$asc<=-19776) return 'B'; 
        if($asc>=-19775&&$asc<=-19219) return 'C'; 
        if($asc>=-19218&&$asc<=-18711) return 'D'; 
        if($asc>=-18710&&$asc<=-18527) return 'E'; 
        if($asc>=-18526&&$asc<=-18240) return 'F'; 
        if($asc>=-18239&&$asc<=-17923) return 'G'; 
        if($asc>=-17922&&$asc<=-17418) return 'H'; 
        if($asc>=-17417&&$asc<=-16475) return 'J'; 
        if($asc>=-16474&&$asc<=-16213) return 'K'; 
        if($asc>=-16212&&$asc<=-15641) return 'L'; 
        if($asc>=-15640&&$asc<=-15166) return 'M'; 
        if($asc>=-15165&&$asc<=-14923) return 'N'; 
        if($asc>=-14922&&$asc<=-14915) return 'O'; 
        if($asc>=-14914&&$asc<=-14631) return 'P'; 
        if($asc>=-14630&&$asc<=-14150) return 'Q'; 
        if($asc>=-14149&&$asc<=-14091) return 'R'; 
        if($asc>=-14090&&$asc<=-13319) return 'S'; 
        if($asc>=-13318&&$asc<=-12839) return 'T'; 
        if($asc>=-12838&&$asc<=-12557) return 'W'; 
        if($asc>=-12556&&$asc<=-11848) return 'X'; 
        if($asc>=-11847&&$asc<=-11056) return 'Y'; 
        if($asc>=-11055&&$asc<=-10247) return 'Z'; 
        return null; 
   }
}


?>